package com.abc.intellihome.controller;


import com.abc.intellihome.domain.Furniture;
import com.abc.intellihome.service.FurnitureService;
import com.abc.intellihome.util.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class FurnitureController {

    @Autowired
    private FurnitureService furnitureService;

    @GetMapping("/furnitures/{username}")
    public ResultInfo getALlFurnitures(@PathVariable String username){
        List<Furniture> furnitureList = furnitureService.chatAll(username);
        ResultInfo resultInfo = ResultInfo.getSuccessInfo();
        resultInfo.setResultData(furnitureList);


        return resultInfo;
    }

    @PostMapping("/change/onOff")
    public void changeOn(@RequestBody Furniture furniture){
        furnitureService.changeOnOff(furniture);

    }

    @PostMapping("/add")
    public ResultInfo add(Furniture furniture){
        int result= furnitureService.addFurniture(furniture);
        ResultInfo resultInfo;
        if (result==1){
            resultInfo = ResultInfo.getSuccessInfo();
            return resultInfo;
        }
        resultInfo=ResultInfo.getFailInfo();
        return resultInfo;

    }

    @DeleteMapping("/furniture")
    public ResultInfo delFurniture(Furniture furniture){
        int result = furnitureService.removeFurniture(furniture);
        ResultInfo resultInfo;
        if (result==1){
            resultInfo = ResultInfo.getSuccessInfo();
            return resultInfo;
        }
        resultInfo=ResultInfo.getFailInfo();
        return resultInfo;
    }



}
